﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Shuba.Web.mobile
{
    public class download_apk : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string version = context.Request["v"];
            if (string.IsNullOrEmpty(version))
            {
                context.Response.StatusCode = 400;
                return;
            }

            ws.mobile ws = new Shuba.Web.ws.mobile();            
            string path = ws.GetUpdatePath(version);
            
            FileInfo info = new FileInfo(path);
            if (!info.Exists)
            {
                context.Response.StatusCode = 404;
                return;
            }            
            
            string fileName = string.Format("SPAMobile_{0}.apk", version);
            Downloader d = new Downloader("application/vnd.android.package-archive", fileName, info.LastWriteTimeUtc, info.Length);

            FileStream fileStream = new FileStream(path,
                FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            d.ExecuteResult(context, fileStream);         
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
